using System;
using System.Collections.Generic;
using System.Text;

namespace ElteIk.RoboSoccer.Core
{
    /// <summary>
    /// SoccerGame with robot players and robot teams.
    /// </summary>
	public class RoboSoccerGame : SoccerGame
	{
		#region Lifecycle

		public RoboSoccerGame()
			: base()
		{
		}
        
        /// <summary>
        /// Constructs the standard 3 vs. 3 game.
        /// </summary>
        public override void Initialize()
        {
            base.Initialize();

            RoboSoccerTeam team1 = new RoboSoccerTeam();
            team1.Id = 0;
            team1.Add(RobotFactory.Create());
            team1.Add(RobotFactory.Create());
            team1.Add(RobotFactory.Create());
            RoboSoccerTeam team2 = new RoboSoccerTeam();
            team2.Id = 1;
            team2.Add(RobotFactory.Create());
            team2.Add(RobotFactory.Create());
            team2.Add(RobotFactory.Create());

            Setup(team1, team2);
            DoPlacements();
        }

		#endregion

		#region Properties

		private List<RobotPlayer> robots = new List<RobotPlayer>();

        /// <summary>
        /// The collection of robots.
        /// </summary>
		public List<RobotPlayer> Robots
		{
			get
			{
				return robots;
			}
		}

        /// <summary>
        /// The robo-team playing at left.
        /// </summary>
        public new RoboSoccerTeam LeftTeam
        {
            get
            {
                return (RoboSoccerTeam) base.LeftTeam;
            }
        }

        /// <summary>
        /// The robo-team playing at right.
        /// </summary>
        public new RoboSoccerTeam RightTeam
        {
            get
            {
                return (RoboSoccerTeam)base.RightTeam;
            }
        }

		#endregion

        /// <summary>
        /// Adds a new player to the game. Only robot players are allowed to be added.
        /// </summary>
        /// <param name="player"></param>
		public override void AddPlayer(IPlayer player)
		{
            if (!(player is RobotPlayer))
                return;

            RobotPlayer robot = (RobotPlayer)player;
			robots.Add(robot);
            Players.Add(player);
            physicsEngine.AddPolygon(robot);
		}

        /// <summary>
        /// Places the players on the field.
        /// </summary>
        /// <param name="left"></param>
        /// <param name="right"></param>
        public virtual void Setup(RoboSoccerTeam left, RoboSoccerTeam right)
        {
            robots.Clear();
            base.Setup(left, right);
        }

        /// <summary>
        /// Starts the game. It needs to have a valid physics engine attached to it.
        /// </summary>
		public override void Start()
		{
			base.Start();
		}

        /// <summary>
        /// Steps the game state forward.
        /// It lets the robots update their inner states too, which shouldn't involve computing tactics at all.
        /// </summary>
        /// <param name="f"></param>
        public override void Step(float f)
        {
            foreach (RobotPlayer robot in robots)
                robot.Update(f);

            time += Convert.ToDecimal(f);

            base.Step(f);
        }

		#region Helper methods

        //private void RebuildRobots()
        //{
        //    robots = new List<RobotPlayer>(16);
        //    foreach (IPlayer player in Players)
        //    {
        //        robots.Add(player as RobotPlayer);
        //    }
        //}

		#endregion

	}
}
